package model.viewModels;

import java.util.ArrayList;
import model.Dare;
import model.User;
import view.ChessFrame;
import view.viewPanels.DaresViewPanel;
import control.Communication;

/**
 * model-class for the dares-view
 */
public class DaresModel {

    /**
     * related view-class
     */
    private DaresViewPanel daresViewPanel;

    /**
     * List of user you can dare
     */
    private ArrayList<User> dareableUsers;

    /**
     * List of your open dares
     */
    private ArrayList<Dare> openDares;

    /**
     * constructor of the DaresModel class
     * 
     * @param daresView
     *            reference to its view panel
     */
    public DaresModel( DaresViewPanel daresView )
    {
	this.daresViewPanel = daresView;
    }

    /**
     * updates the data of all dares
     */
    public final void updateDaresData()
    {
	User logU = CenterModel.loggedInUser;
	// clear all lists
	this.dareableUsers = new ArrayList<User>();
	this.openDares = new ArrayList<Dare>();

	String userNicks = Communication.getUserList( logU.getNick() );
	String sep = "#";
	for ( String s : userNicks.substring( 2 ).split( sep ) )
	{
	    this.dareableUsers.add( new User( s ) );
	}

	String[] openDaresData = Communication.getOpenDares( logU.getId(), logU.getNick() );

	for ( int i = 0; i < openDaresData.length; i++ )
	{
	    this.openDares.add( new Dare( openDaresData[i].split( sep ) ) );
	}

	this.daresViewPanel.updateTables( this.dareableUsers, this.openDares );

    }

    /**
     * sends a new dare to the database and updates the dares data
     * 
     * @param name
     *            name of the user
     * @param opponent
     *            name of the challenged user
     * @param color
     *            color the challenging user wants to play
     * @param msg
     *            the message from the challenging user
     */
    public final void sentDareAction( String name, String opponent, boolean color, String msg )
    {
	Communication.sendDare( name, opponent, color, msg );
	this.updateDaresData();
    }

    /**
     * accepts a dare and updates all data
     * 
     * @param rowIndex
     *            the row index of the accepted dare
     */
    public final void acceptAction( int rowIndex )
    {
	Dare accDare = this.openDares.get( rowIndex );
	String name = CenterModel.loggedInUser.getNick();

	Communication.acceptDare( accDare.getId(), name );

	ChessFrame.updateAction();
    }

    /**
     * declines a dare and updates all data
     * 
     * @param rowIndex
     *            the row index of the declined dare
     */
    public final void declineAction( int rowIndex )
    {
	Dare declDare = this.openDares.get( rowIndex );
	declDare.setDeclined( true );
	String name = CenterModel.loggedInUser.getNick();

	if ( Communication.declineDare( declDare.getId(), name ) )
	{
	    System.out.println( "Ablehnen erfolgreich" );
	}

	// TODO warum nicht:
	// ChessFrame.updateAction();
    }

    public final DaresViewPanel getDaresViewPanel()
    {
	return this.daresViewPanel;
    }

    public final ArrayList<Dare> getOpenDares()
    {
	return this.openDares;
    }

}
